<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <!--edge浏览器H5兼容设置-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!--360浏览器H5兼容设置-->
    <meta name="renderer" content="webkit"/>
    <title>电脑商城</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--导入核心文件-->
    <script src="../bootstrap3/js/holder.js"></script>
    <link href="../bootstrap3/css/bootstrap.css" rel="stylesheet" type="text/css">
    <script src="../bootstrap3/jquery-1.9.1.min.js"></script>
    <script src="../bootstrap3/js/bootstrap.js"></script>
    <!-- 字体图标 -->
    <link rel="stylesheet" href="../bootstrap3/font-awesome-4.7.0/css/font-awesome.css"/>
    <link rel="stylesheet" type="text/css" href="../css/layout.css"/>
    <link rel="stylesheet" type="text/css" href="../css/top.css"/>
    <link rel="stylesheet" type="text/css" href="../css/footer.css"/>
    <link rel="stylesheet" type="text/css" href="../css/order.css"/>
</head>

<body>
<!--头部-->
<header class="header">
    <!--电脑商城logo-->
    <div class="row">
        <div class="col-md-3">
            <a href="index_business.html">
                <img src="../images/index/stumalllogo.png"/>
            </a>
        </div>
        <!--快捷选项-->
        <div class="col-md-9 top-item">
            <ul class="list-inline pull-right">

                <li><a href="orders_business.html"><span class="fa fa-file-text"></span>&nbsp;订单</a></li>
                <li class="li-split">|</li>
                <li><a href="cart.html"><span class="fa fa-cart-plus"></span>&nbsp;购物车</a></li>
                <li class="li-split">|</li>
                <li>
                    <!--下列列表按钮 ：管理-->
                    <div class="btn-group">
                        <button type="button" class="btn btn-link dropdown-toggle" data-toggle="dropdown">
                            <span id="top-dropdown-btn"><span class="fa fa-gears"></span>&nbsp;管理 <span
                                    class="caret"></span></span>
                        </button>
                        <ul class="dropdown-menu top-dropdown-ul" role="menu">
                            <li><a href="password_business.html">修改密码</a></li>
                            <li><a href="userdata_business.html">个人资料</a></li>
                            <li><a href="upload_business.html">上传头像</a></li>
                            <li><a href="address_business.html">收货管理 </a></li>
                        </ul>
                    </div>
                </li>
                <li class="li-split">|</li>
                <li><a href="login.html"><span class="fa fa-user"></span>&nbsp;登录</a></li>
            </ul>
        </div>
    </div>
</header>
<!--导航 -->
<!--分割导航和顶部-->
<div class="row top-nav">
    <div class="col-md-6">
        <ul class="nav nav-pills">
            <li>
                <a href="#"></a>
            </li>
            <li class="active"><a href="index_business.html"><span class="fa fa-home"></span></a></li>
            <form id="form-search" action="search_business.html" class="form-inline pull-right" role="form">
                <div class="form-group">
                    <input type="text" class="form-control" id="search" name="title" placeholder="请输入商品名称进行搜索">
                </div>
                <input id="btn-search" type="submit" class="btn btn-primary" value="搜索" />
            </form>
        </ul>
    </div>
</div>
<!--头部结束-->
<!--导航结束-->
<div class="container">
    <div class="col-md-2">
        <!--左侧导航开始-->
        <div class="panel-group" id="accordion">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <!--主选项：我的订单-->
                    <p class="panel-title">
                        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">我的订单</a>
                    </p>
                </div>
                <div id="collapseOne" class="panel-collapse collapse in">
                    <div class="panel-body">
                        <div><a href="orders.html"><b>全部订单</b></a></div>
                    </div>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-heading">
                    <!--主选项：资料修改-->
                    <p class="panel-title">
                        <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">资料修改</a>
                    </p>
                </div>
            </div>
        </div>
    </div>
    <!--左侧导航结束-->
    <div class="col-md-10">
        <input id="businessId" name="businessId" value="1" size="1" class="hide">
        <div class="panel panel-default">
            <table class="cart-table" width="100%">
                <thead>
                <th width="90%"><input id="refuseReason"type="text" class="form-control"
                                       placeholder="如果拒绝退款，请先写原因，再点击确定内容"></th>
                <th width="10%"><input id="btn-sure" type="button" class="form-control"
                                       value="确定内容"/></th>
                </thead>
            </table>
            <div class="panel-body">
                <table class="orders-table" width="100%">
                    <thead>
                    <tr>
                        <th width="5%">用户id</th>
                        <th width="5%">订单id</th>
                        <th width="20%">商品</th>
                        <th width="7%">数量</th>
                        <th width="7%">销量</th>
                        <th width="15%">支付时间</th>
                        <th width="10%">订单状态</th>
                        <th width="10%">货物状态</th>
                        <th width="20%">操作</th>
                    </tr>
                    </thead>
                    <tbody id="order-list" class="orders-body">
                    <tr>
                        <td>1</td>
                        <td>1</td>

                        <td>联想(Lenovo)小新Air13 Pro 13.3英寸14.8mm超轻薄笔记本电脑</td>
                        <td>¥<span>5298</span></td>
                        <td>1件</td>
                        <td>¥<span>5298</span></td>
                        <td><a href="#">申请售后</a></td>
                        <td>
                            <div>已发货</div>
                            <div><a href="orderInfo.html">订单详情</a></div>
                        </td>
                        <td><a href="#" class="btn btn-default btn-xs">发货</a><a href="#" class="btn btn-default btn-xs">退货</a>
                        </td>
                    </tr>
                    </tbody>
                </table>
                <div>
                    <span id="all-price" class="pull-right">123</span><span class="pull-right">总金额:</span>
                </div>
            </div>
        </div>

    </div>
</div>

</div>
<script type="text/javascript">
    $("#businessId").val(localStorage.getItem("businessId"));
    console.log($("#businessId").val());
    $(document).ready(function () {
        showOrderList();
    });

    function showOrderList() {
        $("#order-list").empty();

        $.ajax({
            url: "/order/business_order",
            type: "GET",
            data: $("#businessId").serialize(),
            dataType: "JSON",
            success: function (json) {

                console.log(json.data);
                let list = json.data;
                console.log("count=" + list.length);

                let allPrice = 0;
                for (let i = 0; i < list.length; i++) {
                    console.log(list[i].uid);
                    let tr = '<tr>\n' +
                        '<td>#{uid}</td>' +
                        '<td>#{oid}</td>' +
                        '<td>#{title}</td>' +
                        '<td>#{num}</td>\n' +
                        '<td>#{priority}</td>' +
                        '<td>#{payTime}</td>' +
                        '<td>#{status}</td>' +
                        '<td>#{pStatus}</td>' +

                        '<td><input id="btn-send" onclick="sendProduct(#{oid},#{pid})" class="btn btn-primary" type="button" value="发货" />' +
                        '<input id="btn-return" onclick="returnProduct(#{oid},#{pid},#{num}),addMoney(#{addAmount},#{uid})" class="btn btn-primary" type="button" value="退货" />' +
                        '<input id="btn-refuse" onclick="refuseProduct(#{oid},#{pid})" class="btn btn-primary" type="button" value="拒绝" /></td>\n' +
                        '</tr>';
                    tr = tr.replace(/#{uid}/g, list[i].uid);
                    tr = tr.replace(/#{oid}/g, list[i].oid);
                    tr = tr.replace(/#{pid}/g, list[i].pid);
                    tr = tr.replace(/#{title}/g, list[i].title);
                    tr = tr.replace(/#{num}/g, list[i].num);
                    tr = tr.replace(/#{priority}/g, list[i].priority);
                    tr = tr.replace(/#{payTime}/g, list[i].payTime);
                    tr = tr.replace(/#{addAmount}/g, list[i].num*list[i].price);

                    // tr = tr.replace(/#{status}/g, list[i].status);
                    // tr = tr.replace(/#{pStatus}/g, list[i].pStatus);

                    console.log(list[i]);
                    if (list[i].status == 0) {
                        tr = tr.replace(/#{status}/g, "未支付 请谨慎发货");
                    } else if (list[i].status == 1) {
                        tr = tr.replace(/#{status}/g, "已支付");
                    } else if (list[i].status == 2) {
                        tr = tr.replace(/#{status}/g, "已收款");
                    } else if (list[i].status == 3) {
                        tr = tr.replace(/#{status}/g, "申请退款");
                    }else if (list[i].status == 4) {
                        tr = tr.replace(/#{status}/g, "已完成");
                    }else {
                        tr = tr.replace(/#{status}/g, "订单异常");
                    }

                    if (list[i].pstatus == 0) {
                        tr = tr.replace(/#{pStatus}/g, "未发货");
                    } else if (list[i].pstatus == 1) {
                        tr = tr.replace(/#{pStatus}/g, "已发货");
                    } else if (list[i].pstatus == 2) {
                        tr = tr.replace(/#{pStatus}/g, "已到货");
                    } else if (list[i].pstatus == 3) {
                        tr = tr.replace(/#{pStatus}/g, "已收货");
                    } else if (list[i].pstatus == 4) {
                        tr = tr.replace(/#{pStatus}/g, "交易完成");
                    }else if (list[i].pstatus == 5) {
                        tr = tr.replace(/#{pStatus}/g, "申请退货");
                    }else if (list[i].pstatus == 6) {
                        tr = tr.replace(/#{pStatus}/g, "退货完成");
                    }else if (list[i].pstatus == 7) {
                        tr = tr.replace(/#{pStatus}/g, "退货驳回");
                    }else {
                        tr = tr.replace(/#{pStatus}/g, "订单异常");
                    }

                    $("#order-list").append(tr);
                }
            }
        });
    }

    function sendProduct(oid, pid) {
        $.ajax({
            url: "/order/business_update",
            type: "POST",
            data: "oid=" + oid + "&pid=" + pid,
            dataType: "JSON",
            success: function (json) {
                if (json.state == 200) {
                    alert("发货成功");

                    showOrderList();
                } else {
                    alert("发货异常")
                }
            },
            error: function (xhr) {
                alert("审核产生未知的异常" + xhr.message);//自动产生错误描述信息
            }
        })
    }

    function returnProduct(oid,pid,num) {
        $.ajax({
            url: "/order/business_return",
            type: "POST",
            data: "oid=" + oid + "&pid=" + pid,
            dataType: "JSON",
            success: function (json) {
                if (json.state == 200) {
                    alert("退货成功");
                    subPriority(oid, pid, num);
                    showOrderList();
                } else {
                    alert("发货异常")
                }
            },
            error: function (xhr) {
                alert("审核产生未知的异常" + xhr.message);//自动产生错误描述信息
            }
        })
    }

    function subPriority(oid, pid, num) {
        $.ajax({
            url: "/order/business_subPriority",
            type: "POST",
            data: "oid=" + oid + "&pid=" + pid + "&num=" + num,
            dataType: "JSON",
            success: function (json) {
                if (json.state == 200) {
                    console.log("销量减少");
                    // alert("销量增加");
                } else {
                    alert("货物异常");
                }
            },
            error: function (xhr) {
                alert("审核产生未知的异常" + xhr.message);//自动产生错误描述信息
            }
        })
    }


    $("#btn-sure").click(function getReply() {
        newRe=$("#refuseReason").val();
        localStorage.removeItem("reason");
        localStorage.setItem("reason","'"+newRe+"'");

        console.log("拒绝理由"+newRe);
    })

    function refuseProduct(oid,pid) {
        $.ajax({
            url: "/order/business_refuse",
            type: "POST",
            data: "oid=" + oid + "&pid=" + pid,
            dataType: "JSON",
            success: function (json) {
                if (json.state == 200) {
                    alert("拒绝成功");
                    showOrderList();
                } else {
                    alert("发货异常")
                }
            },
            error: function (xhr) {
                alert("审核产生未知的异常" + xhr.message);//自动产生错误描述信息
            }
        })
    }

    function addMoney(addAmount,uid) {
        $.ajax({
            url: "/user/userRefund",
            type: "POST",
            data:"addAmount="+addAmount+"&uid="+uid,
            dataType: "JSON",
            success: function (json) {
                if (json.state == 200) {
                    alert("用户退款成功");
                    alert(addAmount);
                    recordInsert(addAmount,uid);
                } else {
                    alert("用户退款失败");
                }
            },
            error: function (xhr) {
                alert("用户信息修改时产生未知的异常" + xhr.message);//自动产生错误描述信息
            }
        })
    }

    function recordInsert(addAmount,uid) {
        $.ajax({
            url: "/user/purse_insert",
            type: "POST",
            data:"uid="+uid+"&username=商家"+"&addAmount="+addAmount,
            dataType: "JSON",
            success: function (json) {
                if (json.state == 200) {
                    alert("记录成功");
                } else {
                    alert("数据不存在");
                }
            },
            error: function (xhr) {
                alert("在查询用户信息时产生未知异常" + xhr.message);//自动产生错误描述信息
            }
        })
    }

</script>
</body>

</html>